package client;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import Dao.UsersDao;
import Dao.Impl.UsersDaoImpl;
import entity.Users;

public class UserServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	
	public void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userName = request.getParameter("name");
		String pwd = request.getParameter("password");
		UsersDao userDao = new UsersDaoImpl();
		int id = Integer.parseInt(userName);
		try {
			Users user = userDao.login(id, pwd);
			if(user != null) {
				PrintWriter out = response.getWriter();
				out.print(1);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		PrintWriter out = response.getWriter();
		out.print(0);
		
	}
	
	public void userInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");		
		String userName = request.getParameter("name");
		String pwd = request.getParameter("password");
		UsersDao userDao = new UsersDaoImpl();
		int id = Integer.parseInt(userName);
		try {
			Users user = userDao.login(id, pwd);
			if(user != null) {
				PrintWriter out = response.getWriter();
				Gson gson = new Gson();
				String str = gson.toJson(user);
				out.print(str);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		PrintWriter out = response.getWriter();
		out.print("error:user invalidate");
	}
	
	
	public void register (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String userStr = request.getParameter("user");
		Gson gson = new Gson();
		Users user = gson.fromJson(userStr, Users.class);
		UsersDao dao = new UsersDaoImpl();
		try {
			String ret = "";
			if(!dao.isExist(user.getUser_no())) {
				dao.add(user);
				ret = "true";
			} else {
				ret = "error:the user has been registed!";
			}
			PrintWriter pw = response.getWriter();
			pw.print(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	public void changePasswrod (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String user_no = request.getParameter("user_no");
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		UsersDao userDao = new UsersDaoImpl();
		int id = Integer.parseInt(user_no);
		PrintWriter out = response.getWriter();

		try {
			Users user = userDao.login(id, oldPassword);
			if(user == null) {
				out.print("error:old passworld invalidate");
			} else {
				user.setUser_password(newPassword);
				userDao.update(user);
				out.print("true");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void checkUserhasExist (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String user_no = request.getParameter("user_no");
		UsersDao dao = new UsersDaoImpl();
		try {
			String ret = "";
			if(dao.isExist(Integer.valueOf(user_no))) {
				ret = "true";
			} else {
				ret = "false";
			}
			PrintWriter pw = response.getWriter();
			pw.print(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
